#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include "data_source.h"
#include "related_products.h"

using namespace std;


void get_related_products(string date_source_path, string result_file_path, size_t max_recommend_num)
{
    DataSource d = DataSource(date_source_path);
    cout << "product num: " << d.product_user_set_map.size() << endl;
    RelatedProducts r = RelatedProducts(d.product_user_set_map, max_recommend_num);
    cout << "recommended product num: " << r.related_products_map.size() << endl;
    ofstream result_file (result_file_path.c_str());
    if (result_file.is_open())
    {
        map<string, SimilarProductsQueue>::iterator it ;
        for (it = r.related_products_map.begin(); it != r.related_products_map.end(); ++it)
        {
            if (it->second.empty())
                continue;
            result_file << it->first;
            while (!it->second.empty())
            {
                result_file << "\t" << it->second.top().first << ":" << it->second.top().second;
                it->second.pop();
            }
            result_file << "\n";
        }
    }
    else cout << "Unable to open file";
}


int main(int argc,char *argv[])
{
    if (argc != 4)
        return -1;
    get_related_products(argv[1], argv[2], atoi(argv[3]));
    return 0;
}

